package com.team.abs.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.team.abs.dao.taskgetImpl;
import com.team.abs.dao.taskinsert;
import com.team.abs.dao.taskinsertImpl;
import com.team.abs.pojo.taskpojo;
import com.team.abs.service.kmeansServiceImp;
import com.team.abs.service.kmeanstitleServiceImp;

@RequestMapping("pipi")
@Controller
public class taskcontroller {
	@Autowired
	private taskinsertImpl task;
	@Autowired
	private taskgetImpl taskget;
	@Autowired
	private kmeansServiceImp kmeans;
	@Autowired
	private kmeanstitleServiceImp titilekemans;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	//删除对应任务
	@RequestMapping(value="/del",method=RequestMethod.POST)
	@ResponseBody
	public  ResponseEntity<String> delitem(@RequestParam(value="task_id",required=true)int task_id){
		int ans=task.del_task_id(task_id);
		
	
		return ans>0?ResponseEntity.ok("ok"):ResponseEntity.ok("undone");
	}
	
	@RequestMapping(value="/search/usertask/{userid}",method=RequestMethod.GET)
	@ResponseBody
	public  ResponseEntity<List<taskpojo>> searchtask(
			@PathVariable (value="userid")int user_id){
		List<taskpojo>list=task.get_taskname_by_user_id(user_id);
		return ResponseEntity.ok(list);
	}
	@RequestMapping(value="/search/content/{task_name}/{page_id}",method=RequestMethod.GET)
	@ResponseBody
	public  ResponseEntity<List<String>> searchcontent(
			@PathVariable (value="task_name")String task_name,
			@PathVariable (value="page_id")int page_id
			) throws UnsupportedEncodingException{

		List<String> ans=stringRedisTemplate.opsForList().range(task_name,page_id*10-10, page_id*10-1);
		for(int i=0;i<ans.size();i++) {
			ans.set(i, new String(ans.get(i).getBytes("UTF-8"),"UTF-8"));
		}

		return ResponseEntity.ok(ans);
	}
	@RequestMapping(value="/search/content_size/{task_name}",method=RequestMethod.GET)
	@ResponseBody
	public  ResponseEntity<Long> searchcontent_size(
			@PathVariable (value="task_name")String task_name
		
			) throws UnsupportedEncodingException{
		Long ans=stringRedisTemplate.opsForList().size(task_name);

		return ResponseEntity.ok(ans);
	}
	@RequestMapping(value="/dokmeans/{type}/{task_id}",method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<List<String>>getkmeans(@PathVariable(value="type")int type,
			@PathVariable(value="task_id")int task_id){
	String path="D:\\textfile\\"+task_id;
		List<String>list=kmeans.dokmeans(type, path);
		//System.out.println(path);
		return ResponseEntity.ok(list);
	}
	
	@RequestMapping(value="/plot/{task_id}",method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<taskpojo>getpojo(
			@PathVariable(value="task_id")int task_id){
		taskpojo datapojo=taskget.taskget_task_id_pojo(task_id);
		return ResponseEntity.ok(datapojo);
	}
	@RequestMapping(value="/getpng/{task_id}",method=RequestMethod.GET)
	  public void getBiImg(
			  @PathVariable(value="task_id")String task_id,
			  HttpServletRequest request, HttpServletResponse response) throws IOException {
		FileInputStream fis = null;
		OutputStream os = null;
		try {
			fis = new FileInputStream("D:\\textfile\\"+task_id+".png");
			os = response.getOutputStream();
			int count = 0;
			byte[] buffer = new byte[10*1024*1024 * 8];
			while ((count = fis.read(buffer)) != -1) {
				os.write(buffer, 0, count);
				os.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


   }
	
	@RequestMapping(value="/kmeans/{task_id}/{kvalue}",method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<List<String>>dokmeans(
			 @PathVariable(value="task_id")String task_id,
			@PathVariable(value="kvalue")int kvalue){
		List<String>st=titilekemans.title(task_id, kvalue);
		StringBuilder sb=new StringBuilder();
		for(String s:st) {sb.append(s);sb.append(" ");}
		String ans=sb.toString();
		task.insert_task_id_res_title(Integer.valueOf(task_id), ans);
		//st.forEach(System.out::println);
		return ResponseEntity.ok(st);
	}
}
